###################################################
##名称：钢筋直螺纹套筒sketchup ruby api类封装代码
##作者：钢之炼筋术士
##日期：2023.6.20 未完成
##测试软件：sketchup pro 2020  
##测试版本：20.0.363 64位
##参考文档：ruby.sketchup.com;www.runoob.com/ruby/
##################################################




class Sleeve

    def initialize(dimRF , sleeveLength ,   #构造函数
                    outsideDim , insideDim ,
                    pitchofscrew  )

        #####构造对应参数######
        @dimRF = dimRF.to_f.mm                             #钢筋直径
        @sleeveLength = sleeveLength.to_f.mm                  #套筒长度
        @outsideDim = outsideDim.to_f.mm                        #外径
        @insideDim = insideDim.to_f.mm                          #内径
        @pitchofscrew = pitchofscrew.to_f.mm                    #牙距
        
        #####换算参数########
        @numofscrew = (@sleeveLength / @pitchofscrew).to_i   #牙数
        @insideRadius  = @insideDim/2                        #内半径
        @halfpitchofscrew = @pitchofscrew/2                  #半牙距
        
        #####缩放参数######
        @scale = @halfpitchofscrew*1.732/@insideRadius+1     #缩放参数
        @tr = Geom::Transformation.scaling @scale, @scale, 1 #缩放变量

        ####群组结构######
        @model = Sketchup.active_model
        @group = @model.active_entities.add_group      #最外层群组
        #@group1 = @group.entities.add_group                    #圆环体群组
        #@group2 = @group.entities.add_group                    #螺旋线1群组
        @xaxis2 = [1,0,0]
        #@group3 = @group.entities.add_group                    #螺旋线2群组
        @xaxis3 = [-1,0,0]

        #####炸开缩放用参数######
        @array
        @arcLine3
        @edges2
    end

    def drawCircularTube     #画圆管
        
        center_point = Geom::Point3d.new([0,0,0])
        vector = Z_AXIS
        p @dimRF,@sleeveLength
        @group1 = @group.entities.add_group

        edges = @group1.entities.add_circle(center_point, vector, @outsideDim/2)      #添加一个直径37mm的圆边线
        face = @group1.entities.add_face(edges)                                       #以边为界添加面
        status = face.pushpull(-@sleeveLength, true)                                  #推拉-60mm形成向上的圆柱体

        edges2 = @group1.entities.add_circle(center_point, vector, @insideRadius)     #在圆柱底部画一个直径23mm的圆边线

        face2 = edges2[0].common_face(edges2[1])                                      #利用两边共面引用内径圆的面
        status = face2.pushpull(@sleeveLength, true)                                  #推拉60mm形成圆环柱

        @group1.entities.erase_entities(face2)                                        #删除内径圆的面

        return @group1
    end

    def drawHelix    #画螺旋线 
        
        def draw1(xaxis,group)
            for i in 0..@numofscrew-1
    
                new_edges = group.entities.add_arc([0,0,@pitchofscrew*i], xaxis, [0,0,1], @insideRadius, 0, 180.degrees)
            
                curve = new_edges.first.curve
                points = curve.vertices.map(&:position)
                points.each_with_index { |pt, j| pt.z = (j * @halfpitchofscrew/(points.length-1))+ @pitchofscrew*i}
                curve.move_vertices(points)
            
                new_edges = group.entities.add_arc([0,0,@pitchofscrew*i+@halfpitchofscrew], xaxis, [0,0,1], @insideRadius, 180.degrees, 360.degrees)
            
                curve = new_edges.first.curve
                points = curve.vertices.map(&:position)
                points.each_with_index { |pt, j| pt.z = (j * @halfpitchofscrew/(points.length-1)) +@halfpitchofscrew + @pitchofscrew*i }
                curve.move_vertices(points)
            
            
            end
        end

        @group2 = @group.entities.add_group                    #螺旋线1群组
        draw1(@xaxis2,@group2)
        #@array = @group2.explode

        @group3 = @group.entities.add_group                    #螺旋线2群组
        draw1(@xaxis3,@group3)
        #@group3.explode

    end

    def explodescale      #缩放螺旋线，拉出螺纹面
        
        @array = @group3.explode
        #puts @array
        @group2.explode
        
        #@edges2 = @array.first.edges
        i = 0
        until @array[i].class == Sketchup::Edge
            i += 100
        end
        #arcLine3 = array[i].all_connected#puts @edges2
        @arcLine3 = @array[i].all_connected
        #puts @arcLine3
        @group1.explode

       # @tr = Geom::Transformation.scaling @scale, @scale, 1
        @group.entities.transform_entities(@tr, @arcLine3)

    end

    def self.create_Sleeve(params)

        #@group.entities.add_circle([0,0,0], [0,0,1], 10) 
        sl = Sleeve.new(params[0],params[1],params[2],params[3],params[4])
        #sl = Sleeve.new()
        p params
        
        sl.drawCircularTube
        sl.drawHelix
        sl.explodescale
        
        return sl


    end

    def self.create_Dialog

        dialog = UI::HtmlDialog.new(
            {
              :dialog_title => "创建钢筋套筒类",
              :preferences_key => "",
              :scrollable => true,
              :resizable => true,
              :width => 600,
              :height => 800,
              :left => 100,
              :top => 100,
              :min_width => 50,
              :min_height => 50,
              :max_width =>1000,
              :max_height => 1000,
              :style => UI::HtmlDialog::STYLE_DIALOG
            })
            dialog.set_file File.join(File.dirname(__FILE__), 'htmldialog/sleeve.html')
            dialog.show
            dialog.add_action_callback("confirm") { |action, params|
                # params即js传过来的值
                create_Sleeve(params)
            }

            dialog.add_action_callback("cancel"){
                dialog.close
            }


    end
end


#sl = Sleeve.create_Sleeve(['25','60','37','23.5','3'])


